home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / WDFTST__ / DIALOGS.C < prev    next >
C/C++ Source or Header  |  1990-06-12  |  5KB  |  201 lines

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <MacProto.h>
  4. #include "proto.h"
  5. #include "defines.h"
  6. #include "external_globals.h"
  7. #include "TextDisplay.h"
  8.  
  9.  
  10. int        g_grow_icon_choice;
  11.  
  12. pascal Boolean Dialog_filter( DialogPtr the_dialog,
  13.                             EventRecord *the_event,
  14.                             int            *item_number );
  15.  
  16. ControlHandle    Get_Ditem_handle( DialogPtr theDialog, int item_no );
  17. void            Tune_radio( DialogPtr what_dialog, int on_button, ...);
  18.  
  19. /* ------------------------- Handle_grow_choice ----------------------- */
  20. void Handle_grow_choice()
  21. {
  22.     DialogPtr        grow_dlog;
  23.     int                item_type;
  24.     Rect            item_box;
  25.     int                item_hit;
  26.     int                t_choice;
  27.     Boolean            alldone;
  28.     
  29.     grow_dlog = GetNewDialog( GROW_ICON_DLOG_ID, nil, (WindowPtr)-1L );
  30.     Tune_radio( grow_dlog, g_grow_icon_choice, grow_clipped,
  31.         grow_standard, grow_none, 0 );
  32.     TD_activate( g_event_window, FALSE );
  33.     ShowWindow( grow_dlog );
  34.     alldone = FALSE;
  35.     t_choice = g_grow_icon_choice;
  36.     while (!alldone)
  37.     {
  38.         ModalDialog( (ProcPtr)Dialog_filter, &item_hit );
  39.         switch( item_hit )
  40.         {
  41.             case grow_ok:
  42.                 alldone = TRUE;
  43.                 break;
  44.             case grow_cancel:
  45.                 alldone = TRUE;
  46.                 break;
  47.             case grow_clipped:
  48.             case grow_standard:
  49.             case grow_none:
  50.                 t_choice = item_hit;
  51.                 Tune_radio( grow_dlog, t_choice, grow_clipped,
  52.                     grow_standard, grow_none, 0 );
  53.                 break;
  54.         }
  55.     }
  56.     DisposDialog( grow_dlog );
  57.     if (item_hit == grow_ok)
  58.     {
  59.         g_grow_icon_choice = t_choice;
  60.         SetPort( g_first_window );
  61.         InvalRect( &thePort->portRect );
  62.         SetPort( g_second_window );
  63.         InvalRect( &thePort->portRect );
  64.     }
  65. }
  66.  
  67. /* --------------------- Get_Ditem_handle -------------------------- */
  68.  
  69. ControlHandle
  70. Get_Ditem_handle( DialogPtr theDialog, int item_no )
  71. {
  72.     int item_type;
  73.     Rect box;
  74.     Handle item_handle;
  75.     
  76.     GetDItem( theDialog, item_no, &item_type, &item_handle, &box );
  77.     return( (ControlHandle) item_handle );
  78. }
  79.  
  80. /* ----------------------- Tune_radio ----------------------- */
  81. /* 
  82. *    Switch one radio button on and some others off.
  83. *    Input: a handle to the dialog, the item number of the button
  84. *    to turn on, a list of item numbers for buttons to turn off,
  85. *    and a zero to terminate the list.  The "on" button isn't turned
  86. *    on until the end, so if the first item also occurs later on
  87. *    the list, it will end up turned on.  Thus, to reset a radio
  88. *    group, you can list the entire group after the first item.
  89. */
  90. void
  91. Tune_radio( DialogPtr what_dialog, int on_button, ...)
  92. {
  93.     va_list    arg_ptr;
  94.     ControlHandle    button_h;
  95.     int     off_button;
  96.     
  97.     va_start( arg_ptr, on_button );
  98.     while ( (off_button = va_arg(arg_ptr, int)) > 0 )
  99.     {
  100.         button_h = Get_Ditem_handle( what_dialog, off_button );
  101.         SetCtlValue( button_h, (int)FALSE );
  102.     }
  103.     va_end( arg_ptr );
  104.     button_h = Get_Ditem_handle( what_dialog, on_button );
  105.     SetCtlValue( button_h, (int)TRUE );
  106. }
  107.  
  108. /* -------------------------- Handle_title_choice ---------------------- */
  109. typedef enum {
  110.     td_ok_item = 1,
  111.     td_cancel_item,
  112.     td_first_title_item,
  113.     td_second_title_item
  114. };
  115.  
  116. void Handle_title_choice()
  117. {
  118.     DialogPtr    title_DLOG;
  119.     Handle         first_item_h, second_item_h;
  120.     int            item_type, item_hit;
  121.     Rect        item_box;
  122.     Str255        new_first_title, new_second_title;
  123.     
  124.     title_DLOG = GetNewDialog( TITLE_DLOG_ID, nil,
  125.         (WindowPtr)MOVE_TO_FRONT );
  126.     GetDItem( title_DLOG, td_first_title_item,
  127.         &item_type, &first_item_h, &item_box );
  128.     SetIText( first_item_h, g_first_window_title );
  129.     GetDItem( title_DLOG, td_second_title_item,
  130.         &item_type, &second_item_h, &item_box );
  131.     SetIText( second_item_h, g_second_window_title );
  132.     SelIText( title_DLOG, td_first_title_item, 0, 32767 );
  133.     TD_activate( g_event_window, FALSE );
  134.     ShowWindow( title_DLOG );
  135.     ModalDialog( (ProcPtr)Dialog_filter, &item_hit );
  136.     if (item_hit == td_ok_item)
  137.     {
  138.         GetIText( first_item_h, new_first_title );
  139.         GetIText( second_item_h, new_second_title );
  140.         Copy_p_string( g_first_window_title, new_first_title );
  141.         Copy_p_string( g_second_window_title, new_second_title );
  142.         Destroy_windows();
  143.         CloseDialog( title_DLOG );
  144.         Make_windows();
  145.     }
  146.     else
  147.         CloseDialog( title_DLOG );
  148. }
  149.  
  150. #define RETURN_KEY    0x24
  151. #define ENTER_KEY    0x4C
  152. #define TILDE_KEY    0x32
  153. #define ESCAPE_KEY    0x35
  154.  
  155. /* ------------------------- Dialog_filter ------------------------ */
  156. pascal Boolean Dialog_filter( DialogPtr the_dialog,
  157.                             EventRecord *the_event,
  158.                             int            *item_number )
  159. {
  160.     int        keycode, charcode;
  161.     char    the_char;
  162.     
  163.     if (the_event->what != keyDown)
  164.         return( FALSE );
  165.     
  166.     keycode = (the_event->message & keyCodeMask) >> 8;
  167.     if ( (keycode == RETURN_KEY) || (keycode == ENTER_KEY) )
  168.     {
  169.         *item_number = 1;    /* OK */
  170.         return( TRUE );
  171.     }
  172.     if ( (keycode == TILDE_KEY) || (keycode == ESCAPE_KEY) )
  173.     {
  174.         *item_number = 2;    /* Cancel */
  175.         return( TRUE );
  176.     }
  177.     
  178.     if (the_event->modifiers & cmdKey)
  179.     {
  180.         charcode = the_event->message & charCodeMask;
  181.         the_char = (char)charcode;
  182.         
  183.         *item_number = ((DialogPeek)the_dialog)->editField + 1; /* current text box */
  184.         switch (charcode)
  185.         {
  186.             case 'x':
  187.                 DlgCut( the_dialog );
  188.                 break;
  189.             case 'c':
  190.                 DlgCopy( the_dialog );
  191.                 break;
  192.             case 'v':
  193.                 DlgPaste( the_dialog );
  194.                 break;
  195.         }
  196.         the_event->what = 0;    /* Change it to a null event */
  197.         return( FALSE );
  198.     }
  199.     else
  200.         return( FALSE );    /* ordinary keystroke */
  201. }